<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')"
             :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
             @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-form-item label="民宿名称" prop="location">
        <el-input v-model="dataForm.location" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="briefIntroduction">
        <el-input v-model="dataForm.briefIntroduction" placeholder="简介"></el-input>
      </el-form-item>
      <el-form-item label="民宿类型" prop="dictId">
        <el-select v-model="dataForm.dictId" clearable placeholder="请选择">
          <el-option
              v-for="item in dictIds"
              :key="item.dictLabel"
              :label="item.label"
              :value="item.dictLabel">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="dataForm.state" placeholder="请选择">
          <el-option
              v-for="item in states"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import Cookies from "js-cookie";

export default {
  data() {
    return {
      visible: false,
      busType: [],
      dictIds: [],
      states: [{
        value: '1',
        label: '启用'
      }, {
        value: '2',
        label: '禁用'
      }],
      dataForm: {
        id: '',
        location: '',
        briefIntroduction: '',
        deptId:'',
        dictId: '',
        state: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
      }
    }
  },
  computed: {
    dataRule() {
      return {
        location: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        briefIntroduction: [
          {required: false, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        dictId: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        state: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  }, mounted () {
    this.getDicList();
    this.getDictId();
  },
  methods: {
    init() {
      this.visible = true
      let fid = "&fid="+this.dataForm.id;
      this.url = `${window.SITE_CONFIG['apiURL']}/biz/filestore/upload?token=${Cookies.get('token')+fid}`
      this.num = 0
      this.fileList = []
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/biz/homestayinformation/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/biz/homestayinformation/',
            this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),

    getDicList: debounce(function () {
      //位置
      let dictTypeId = "1160061077912858625";
      this.$http.get(`/sys/dict/data/getDataByPid?dictTypeId=`+dictTypeId).then(
          ({data: res}) => {
            this.busType = res.data
          })
    }),
    getDictId: debounce(function () {
      //位置
      let dictTypeId = "1623131500523868162";
      this.$http.get(`/sys/dict/data/getDataByPid?dictTypeId=`+dictTypeId).then(
          ({data: res}) => {
            this.dictIds = res.data
          })
    }),
  }
}
</script>
